#pragma once
#include "db.hpp"
#include <unordered_map>
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>

typedef websocketpp::server<websocketpp::config::asio> wsserver_t;

class online_manager
{
public:
    void enter_game_hall(int uid,const wsserver_t::connection_ptr& conn)
    {
        std::unique_lock<std::mutex> ul(_mutex);
        _hall_user.insert(std::make_pair(uid, conn));
    }

    void enter_game_room(int uid,const wsserver_t::connection_ptr& conn)
    {
        std::unique_lock<std::mutex> ul(_mutex);
        _room_user.insert(std::make_pair(uid, conn));
    }

    void exit_game_hall(int uid)
    {
        std::unique_lock<std::mutex> ul(_mutex);
        _hall_user.erase(uid);
    }

    void exit_game_room(int uid)
    {
        std::unique_lock<std::mutex> ul(_mutex);
        _room_user.erase(uid);
    }

    bool is_in_game_hall(int uid)
    {
        std::unique_lock<std::mutex> ul(_mutex);
        auto it = _hall_user.find(uid);
        if (it == _hall_user.end())
        {
            return false;
        }
        return true;
    }

    bool is_in_game_room(int uid)
    {
        std::unique_lock<std::mutex> ul(_mutex);
        auto it = _room_user.find(uid);
        if (it == _room_user.end())
        {
            return false;
        }
        return true;
    }

    wsserver_t::connection_ptr get_conn_from_hall(int uid)
    {
        std::unique_lock<std::mutex> ul(_mutex);
        auto it = _hall_user.find(uid);
        if (it == _hall_user.end())
        {
            return wsserver_t::connection_ptr();
        }
        return it->second;
    }
    
    wsserver_t::connection_ptr get_conn_from_room(int uid)
    {
        std::unique_lock<std::mutex> ul(_mutex);
        auto it = _room_user.find(uid);
        if (it == _room_user.end())
        {
            return wsserver_t::connection_ptr();
        }
        return it->second;
    }

private:
    std::unordered_map<int, wsserver_t::connection_ptr> _hall_user;
    std::unordered_map<int, wsserver_t::connection_ptr> _room_user;
    std::mutex _mutex;
};